package spring.cloud.tasks.executor.service.impl;

import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.NodeInfo;
import spring.cloud.tasks.core.task.service.TaskRuntimeService;
import spring.cloud.tasks.executor.service.PauseOrResumeScheduleTaskService;
import spring.cloud.tasks.executor.service.TaskHeartBeatQueueService;
import spring.cloud.tasks.executor.service.TaskManager;
import spring.cloud.tasks.metadata.api.assign.entity.TaskAssignResultEntity;
import spring.cloud.tasks.metadata.api.task.entity.TaskEntity;
import spring.cloud.tasks.metadata.api.task.entity.TaskRuntimeEntity;
import spring.cloud.tasks.metadata.api.task.enums.ScheduleServerStatusEnum;

import javax.annotation.Resource;
import java.util.Date;


@Slf4j
public class TaskManagerFactory {

    @Resource
    private NodeInfo currentNode;
    @Resource
    private TaskRuntimeService taskRuntimeService;
    @Resource
    private TaskHeartBeatQueueService taskHeartBeatQueueService;
    @Resource
    private PauseOrResumeScheduleTaskService pauseOrResumeScheduleTaskService;

    public TaskManager create(TaskEntity taskEntity, TaskAssignResultEntity taskAssignResultEntity) throws Exception {
        TaskRuntimeEntity taskRuntimeEntity = null;
        TaskManager taskManager = null;
        try {
            taskRuntimeEntity = createTaskRuntimeInfo(taskEntity, taskAssignResultEntity);
            taskManager = new TaskManagerImpl(taskEntity, taskRuntimeEntity);
            taskManager.registerHeartBeatTask(taskHeartBeatQueueService);
            taskManager.registerDaemonTask(pauseOrResumeScheduleTaskService);
            taskRuntimeEntity.setMessage("初始化成功!");
        } catch (Exception e) {
            log.error("taskName:{},seqNo:{} 启动异常!", taskEntity.getTaskName(), taskAssignResultEntity.getSeqNo());
            try {
                if (taskManager != null) {
                    taskManager.unRegisterHeartBeatTask(taskHeartBeatQueueService);
                    taskManager.unRegisterDaemonTask(pauseOrResumeScheduleTaskService);
                }
                if (taskRuntimeEntity != null) {
                    taskRuntimeEntity.setState(ScheduleServerStatusEnum.error.toString());
                    taskRuntimeEntity.setMessage("启动异常:" + e.getMessage());
                    taskRuntimeService.updateTaskRuntimeHeartBeatTime(taskRuntimeEntity);
                }
            } catch (Exception e1) {
                log.error("更新任务运行信息异常", e1);
            }
            throw e;
        }
        return taskManager;
    }


    private TaskRuntimeEntity createTaskRuntimeInfo(TaskEntity taskEntity, TaskAssignResultEntity taskAssignResultEntity) {
        TaskRuntimeEntity taskRuntimeEntity = new TaskRuntimeEntity(taskEntity);
        taskRuntimeEntity.setTaskItems(taskAssignResultEntity.getTaskItems());
        taskRuntimeEntity.setSeqNo(taskAssignResultEntity.getSeqNo());
        taskRuntimeEntity.setAddress(currentNode.getAddress());
        taskRuntimeEntity.setHostName(currentNode.getHostName());
        taskRuntimeEntity.setHeartBeatTime(new Date());
        TaskRuntimeEntity dbTaskRuntime = taskRuntimeService.taskRuntimeIsExist(taskRuntimeEntity);
        if (dbTaskRuntime != null) {
            taskRuntimeEntity.setId(dbTaskRuntime.getId());
        } else {
            taskRuntimeService.insert(taskRuntimeEntity);
        }
        return taskRuntimeEntity;
    }
}
